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 dkjsonInstall 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.rockList Installed Packages
See what’s currently installed:
# List all installed packages
luarocks list
# Show details about a specific package
luarocks show dkjsonRemove Packages
Uninstall packages you no longer need:
# Remove a package
luarocks remove dkjson
# Force remove (ignore dependencies)
luarocks remove --force dkjsonTree 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 luafilesystemCustom 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 listPackage Information
Package Details
Get detailed information about packages:
# Show package details
luarocks show dkjson
# Show available versions
luarocks search dkjson --allDependency Tree
See what dependencies a package has:
# Show dependencies
luarocks deps dkjsonUpdating Packages
Keep your packages up to date:
# Update a specific package
luarocks install --update dkjson
# Update all packages
luarocks admin manifest --update-treePractical 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 lapisTesting Tools
# Install testing framework
luarocks install busted
# Install code formatter
luarocks install luaformatter
# Install static analyzer
luarocks install luacheckData 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-mysqlCommon 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'}" > rockspecEnvironment 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 --outdatedConfiguration
Lua Path
LuaRocks automatically configures Lua’s package paths. You can check:
luarocks pathThis 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 mypackageBest Practices
- Use –local for project-specific packages
- Check dependencies before installing new packages
- Keep packages updated for security and bug fixes
- Use specific versions for production environments
- Document your dependencies in rockspec files
Troubleshooting
Permission Errors
# Fix permission issues
sudo chown -R $USER ~/.luarocksPath 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 dkjsonNext 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.