Using LuaRocks

Once LuaRocks is installed, you can use it to install, manage, and update Lua packages. This guide covers the essential commands you’ll need.

Basic Commands

Search for Packages

Find available packages:

# Search for packages
luarocks search http

# Search for a specific package
luarocks search dkjson

Install Packages

Install packages from the repository:

# Install a package
luarocks install dkjson

# Install a specific version
luarocks install luafilesystem 1.8.0-1

# Install from a local rock file
luarocks install ./mypackage-1.0-1.rock

List Installed Packages

See what’s currently installed:

# List all installed packages
luarocks list

# Show details about a specific package
luarocks show dkjson

Remove Packages

Uninstall packages you no longer need:

# Remove a package
luarocks remove dkjson

# Force remove (ignore dependencies)
luarocks remove --force dkjson

Tree Management

LuaRocks can use different trees for different purposes:

System vs User Installation

# Install for all users (system-wide)
sudo luarocks install luafilesystem

# Install for current user only
luarocks install --local luafilesystem

Custom Trees

Create separate trees for different projects:

# Create a new tree
luarocks --tree=/path/to/mytree install dkjson

# Use the tree
luarocks --tree=/path/to/mytree list

Package Information

Package Details

Get detailed information about packages:

# Show package details
luarocks show dkjson

# Show available versions
luarocks search dkjson --all

Dependency Tree

See what dependencies a package has:

# Show dependencies
luarocks deps dkjson

Updating Packages

Keep your packages up to date:

# Update a specific package
luarocks install --update dkjson

# Update all packages
luarocks admin manifest --update-tree

Practical Examples

Web Development Setup

# Install HTTP client
luarocks install lua-resty-http

# Install JSON handling
luarocks install dkjson

# Install URL encoding
luarocks install lua-cjson

# Install a web framework
luarocks install lapis

Testing Tools

# Install testing framework
luarocks install busted

# Install code formatter
luarocks install luaformatter

# Install static analyzer
luarocks install luacheck

Data Processing

# Install CSV handling
luarocks install csv

# Install YAML support
luarocks install lyaml

# Install database drivers
luarocks install lua-sql-postgres
luarocks install lua-sql-mysql

Common Workflows

Project Setup

# Create a project directory
mkdir myproject
cd myproject

# Install packages locally
luarocks install --local dkjson
luarocks install --local luafilesystem

# Create a rockspec for dependencies
echo "dependencies = {'dkjson', 'luafilesystem >= 1.6'}" > rockspec

Environment Management

# Check what's installed locally
luarocks --local list

# Install packages in development mode
luarocks install --local --dev busted

# Clean up old packages
luarocks purge --outdated

Configuration

Lua Path

LuaRocks automatically configures Lua’s package paths. You can check:

luarocks path

This shows the paths that should be added to your LUA_PATH and LUA_CPATH.

Custom Repositories

Add custom repositories:

# Add a repository
luarocks config repositories.myrepo https://myrepo.example.com

# Install from custom repository
luarocks install --server=myrepo mypackage

Best Practices

  1. Use –local for project-specific packages
  2. Check dependencies before installing new packages
  3. Keep packages updated for security and bug fixes
  4. Use specific versions for production environments
  5. Document your dependencies in rockspec files

Troubleshooting

Permission Errors

# Fix permission issues
sudo chown -R $USER ~/.luarocks

Path Issues

# Check Lua paths
lua -e "print(package.path)"
lua -e "print(package.cpath)"

# Add LuaRocks paths if needed
eval $(luarocks path --bin)

Network Issues

# Use different mirror
luarocks install --server=http://rocks.moonscript.org dkjson

Next Steps

Now that you know how to use LuaRocks, learn how to create your own rocks to share your Lua modules with the community.

For more advanced usage, see the LuaRocks documentation.

Last updated on